﻿using System;
using System.Collections.Generic;
using NpcFramework.Traffic;
using UnityEditor;
using UnityEngine;

namespace NpcFramework
{
    public enum BranchType
    {
        Forward = 1,
        Backward = 2,
    }
    
    //[ExecuteInEditMode]
    public class NpcWayPoint : MonoBehaviour
    {
        [DisplayOnly]
        public int Index;
        [DisplayOnly]
        public NpcWayPoint PrevWayPoint;
        [DisplayOnly]
        public NpcWayPoint NextWayPoint;

        public List<NpcWayPoint> branchPoints = new List<NpcWayPoint>();
        public BranchType branchType = BranchType.Forward;
        [Range(0f, 1f)]
        public float branchRatio = 0.5f;

        public TrafficLight TrafficLight;        // 是否关联了红绿灯（人行道前置点）
        
        [Range(0f, 8f)]
        public float width = 3f;
        public bool IsGenerateCar;
        public bool IsGenerateNpc;
        public string BTreePath;

        public NpcWayPath WayPath;
        
#if UNITY_EDITOR
        private void OnDrawGizmos()
        {

            if (IsGenerateCar)
            {
                Gizmos.color = Color.white;
            }
            else
            {
                Gizmos.color = Color.yellow;
            }

            if (WayPath.IsBranchPoint(this))
            {
                Gizmos.color = Color.red;
            }

            if (IsGenerateNpc)
            {
                Gizmos.color = Color.cyan;
                Gizmos.DrawCube(transform.position + Vector3.up * 0.5f, new Vector3(0.5f, 1.2f, 0.5f));
            }
            else
            {
                Vector3 direction ;
                if (branchType == BranchType.Backward)
                {
                    direction = -transform.forward;
                    if (PrevWayPoint != null)
                    {
                        direction = transform.position - PrevWayPoint.transform.position;
                    }
                }
                else
                {
                    direction = transform.forward;
                    if (NextWayPoint != null)
                    {
                        direction = NextWayPoint.transform.position - transform.position;
                    }
                }
                

                Gizmos.matrix = Matrix4x4.TRS(transform.position + Vector3.up * 0.5f, Quaternion.FromToRotation(Vector3.forward, -direction),
                    Vector3.one * 0.5f);
                Gizmos.DrawFrustum(Vector3.zero, 10f, 2f, 0f, 5f); // x, width, length, x, x



                Gizmos.matrix = Matrix4x4.TRS(transform.position + Vector3.up * 0.5f, Quaternion.FromToRotation(Vector3.forward, direction),
                    Vector3.one * 0.5f);
                Gizmos.DrawCube(Vector3.zero, new Vector3(0.5f, 1f, 0.5f));

            }
        }

        private void OnDestroy()
        {
            if (NextWayPoint)
            {
                Selection.activeGameObject = NextWayPoint.gameObject;
            }
            
//            if (WayPath)
//            {
//                WayPath.DeleteIndexPoint(this,true);
//            }
        }
        
#endif
    }
}
